/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.personnel.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.mybatis.SpliceQueryWrapper;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.personnel.api.entity.Resume;
import com.fuyao.cloud.personnel.api.vo.ResumeExcelVO;
import com.fuyao.cloud.personnel.api.vo.ResumeSearchVO;
import com.fuyao.cloud.personnel.service.ResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 个人简历信息
 *
 * @author 碎月
 * @date 2021-12-16 10:35:30
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/resume" )
@Api(value = "resume", tags = "个人简历信息管理")
public class ResumeController {

    private final ResumeService resumeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param resume 个人简历信息
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('personnel_resume_view')" )
    public R getFyPersonnelResumePage(Page page, ResumeSearchVO resume) {
        return R.ok(resumeService.page(page, SpliceQueryWrapper.spliceSearchCondition(resume)));
    }


    /**
     * 通过id查询个人简历信息
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('personnel_resume_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(resumeService.getById(id));
    }

    /**
     * 新增个人简历信息
     * @param resume 个人简历信息
     * @return R
     */
    @ApiOperation(value = "新增个人简历信息", notes = "新增个人简历信息")
    @SysLog("新增个人简历信息" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('personnel_resume_add')" )
    public R save(@RequestBody Resume resume) {
        return R.ok(resumeService.save(resume));
    }

    /**
     * 修改个人简历信息
     * @param resume 个人简历信息
     * @return R
     */
    @ApiOperation(value = "修改个人简历信息", notes = "修改个人简历信息")
    @SysLog("修改个人简历信息" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('personnel_resume_edit')" )
    public R updateById(@RequestBody Resume resume) {
        return R.ok(resumeService.updateById(resume));
    }

    /**
     * 通过id删除个人简历信息
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除个人简历信息", notes = "通过id删除个人简历信息")
    @SysLog("通过id删除个人简历信息" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('personnel_resume_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(resumeService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param resume 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @PostMapping("/export")
    @PreAuthorize("@pms.hasPermission('personnel_resume_export')" )
    public List<ResumeExcelVO> export(Resume resume) {
        List<Resume> resumeList = resumeService.list(Wrappers.query(resume));
        if(CollUtil.isNotEmpty(resumeList)){
            return resumeList.stream().map(resumeTemp -> {
                ResumeExcelVO excelVO = new ResumeExcelVO();
                BeanUtil.copyProperties(resumeTemp, excelVO);
                return excelVO;
            }).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    /**
     * 导入
     * @return R
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('personnel_resume_export')")
    public R importResume(@RequestExcel List<ResumeExcelVO> excelVOList, BindingResult bindingResult) {
        return resumeService.importResumeInfo(excelVOList, bindingResult);
    }
}
